package com.excuse.excusebuilder.presenter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.excuse.excusebuilder.base.IBaseList;
import com.excuse.excusebuilder.base.ToastQueryListener;
import com.excuse.excusebuilder.commen.util.NetWorkUtil;
import com.excuse.excusebuilder.impl.internet.WifiConnectedListener;
import com.excuse.excusebuilder.bean.Option;
import com.excuse.excusebuilder.bean.Type;
import com.excuse.excusebuilder.view.activity.AddExcuseActivity;
import com.excuse.excusebuilder.view.activity.AddOptionActivity;
import com.excuse.excusebuilder.view.activity.ExcuseActivity;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;

/**
 * Created by 鹏君 on 2017/4/18.
 */

public class HomeFragmentPresenter implements WifiConnectedListener {
    private Context context;
    private IView fragment;
    private List<Type> types = new ArrayList<>();

    public HomeFragmentPresenter(Context context, IView fragment) {
        this.context = context;
        this.fragment = fragment;
    }

    //获取列表信息
    public void getList() {
        if (!NetWorkUtil.isNetWorkConnected(context, this)) return;
        getListFromBmob();
    }

    private void getListFromBmob() {
        //得到所有的Option，每一个Option指定一个Type
        BmobQuery<Option> query = new BmobQuery<>();
        query.include("type");
        query.findObjects(new ToastQueryListener<Option>(context, fragment) {
            @Override
            public void onSuccess(List<Option> list) {
                //得到所有的Type,没有重复

                for (int i = 0; i < list.size(); i++) {
                    Type type_from_list = list.get(i).getType();
                    boolean hasType = false;
                    for (int j = 0; j < types.size(); j++) {
                        if (type_from_list.getObjectId().equals(types.get(j).getObjectId())) {
                            hasType = true;
                            break;
                        }
                    }
                    if (!hasType) {
                        types.add(type_from_list);
                    }
                }

                Log.e("TAG", "types:" + types.size());
                Log.e("TAG", "options:" + list.size());

                List<List<Option>> dataList = new ArrayList<List<Option>>();
                for (int i = 0; i < types.size(); i++) {
                    Type type = types.get(i);
                    List<Option> list_2 = new ArrayList<Option>();
                    for (int j = 0; j < list.size(); j++) {
                        if (type.getObjectId().equals(list.get(j).getType().getObjectId())) {
                            list_2.add(list.get(j));
                        }
                    }
                    dataList.add(list_2);
                }
                fragment.setList(types, dataList);
            }
        });
    }


    public void enterAddExcuseActivity() {
        Intent intent = new Intent(context, AddExcuseActivity.class);
        context.startActivity(intent);
    }

    public void enterExcuseActivity(Option option) {
        Intent intent = new Intent(context, ExcuseActivity.class);
        intent.putExtra(ExcuseActivity.PARAM_OPTION, option);
        context.startActivity(intent);
    }

    @Override
    public void wifiConnected() {
        getListFromBmob();
    }

    public void enterAddOptionActivity(Type type) {
        Intent intent = new Intent(context, AddOptionActivity.class);
        intent.putExtra(AddOptionActivity.PARAM_TYPE, type);
        context.startActivity(intent);
    }

    public interface IView extends IBaseList {
        void setList(List<Type> types, List<List<Option>> dataList);
    }
}
